package be.fedict.eid.diagnostics.tests;

import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;

import be.fedict.eid.diagnostics.DiagnosticTest;
import be.fedict.eid.diagnostics.TestFailedException;
import be.fedict.eid.diagnostics.annotations.TestName;
import be.fedict.eid.diagnostics.helper.DataTreeBuilder;
import be.fedict.eid.diagnostics.system.OSException;
import be.fedict.eid.diagnostics.system.OperatingSystem;
import be.fedict.eid.diagnostics.system.data.SystemService;

@TestName("Data Acquisition: Services")
public class GatherServices extends DiagnosticTest {

	
	protected boolean runTest() throws TestFailedException {
		
		OperatingSystem os;
		try {
			os = new OperatingSystem();
		} catch (OSException e) {
			throw new TestFailedException("Could not establish type of Operating System.");
		}
		
		List<SystemService> serviceList = os.getServiceManager().getServices();
		
		if (serviceList.isEmpty())
			throw new TestFailedException("Could not retrieve list of services");
		
		
		DefaultMutableTreeNode serviceNode;
		for (SystemService systemService : serviceList) {
			serviceNode = DataTreeBuilder.buildNode("service", systemService.getDisplayName());
			serviceNode.add(DataTreeBuilder.buildNode("state", systemService.getStatus().toString()));
			rootNode.add(serviceNode);
		}
		
		return true;
		
	}

	protected String getRootName() {
		return "services";
	}

}
